interface AuthState {
    isAuth: boolean
    token: string,
    isAdmin: boolean,
}

const useAuthStore = defineStore('auth', {
    state: (): AuthState => ({
        isAuth: false,
        token: '',
        isAdmin: false
    }),
    actions: {
        login(token: string, isAdmin: boolean) {
            this.isAuth = true
            this.token = token
            this.isAdmin = isAdmin
            localStorage.setItem('token', token);
            localStorage.setItem('isAdmin', JSON.stringify(isAdmin));
        },
        exit() {
            this.isAuth = false
            this.token = ''
            localStorage.removeItem('token');
            localStorage.removeItem('isAdmin');
        }
    },
    getters: {
        getAuth(state) {
            return {
                isAuth: state.isAuth,
                isAdmin: state.isAdmin,
                token: state.token
            }
        },
        getToken(state) {
            return state.token
        },
        getIsAdmin(state) {
            return state.isAdmin
        }
    }
})

export default useAuthStore
